    //
//  LoginViewController.m
//  Order
//
//  Created by funny on 12-3-22.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "LoginViewController.h"

#import "OrderViewController.h"

#import "UserDBManager.h"

@implementation LoginViewController

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	self.title = @"重新登录";
	
	UIControl *backView = [[[UIControl alloc] initWithFrame:self.view.bounds] autorelease];
	backView.backgroundColor = [UIColor clearColor];
	[backView addTarget:self action:@selector(backTap:) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:backView];
	
	UILabel *nameLabel = [[[UILabel alloc] initWithFrame:CGRectMake(250.0f, 150.0f, 200.0f, 50.0f)] autorelease];
	nameLabel.text = @"用户名：";
	nameLabel.backgroundColor= [UIColor clearColor];
	nameLabel.font = [UIFont systemFontOfSize:40.0f];
	nameLabel.textAlignment = UITextAlignmentCenter;
	[self.view addSubview:nameLabel];
	
	nameTextField = [[[UITextField alloc] initWithFrame:CGRectMake(450.0f, 150.0f, 300.0f, 50.0f)] autorelease];
	nameTextField.placeholder = @"";
	nameTextField.borderStyle = UITextBorderStyleRoundedRect;
	nameTextField.font = [UIFont systemFontOfSize:40.0f];
	nameTextField.textAlignment = UITextAlignmentLeft;
	[self.view addSubview:nameTextField];
	
	UILabel *keyLabel = [[[UILabel alloc] initWithFrame:CGRectMake(250.0f, 240.0f, 200.0f, 50.0f)] autorelease];
	keyLabel.text = @"密码：";
	keyLabel.backgroundColor= [UIColor clearColor];
	keyLabel.font = [UIFont systemFontOfSize:40.0f];
	keyLabel.textAlignment = UITextAlignmentCenter;
	[self.view addSubview:keyLabel];
	
	keyTextField = [[[UITextField alloc] initWithFrame:CGRectMake(450.0f, 240.0f, 300.0f, 50.0f)] autorelease];
	keyTextField.placeholder = @"";
	keyTextField.borderStyle = UITextBorderStyleRoundedRect;
	keyTextField.font = [UIFont systemFontOfSize:40.0f];
	keyTextField.textAlignment = UITextAlignmentLeft;
	keyTextField.secureTextEntry = YES;
	[self.view addSubview:keyTextField];
	
	UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	[loginBtn setTitle:@"登录" forState:UIControlStateNormal];
	loginBtn.titleLabel.font = [UIFont systemFontOfSize:25.0f];
	loginBtn.frame = CGRectMake(300.0f, 340.0f, 300.0f, 50.0f);
	loginBtn.center = CGPointMake(self.view.frame.size.width / 2.0f, loginBtn.center.y);
	[loginBtn addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:loginBtn];
}

- (void)backTap:(id)sender{
	[nameTextField resignFirstResponder];
	[keyTextField resignFirstResponder];
}

- (void)login:(id)sender{
	[nameTextField resignFirstResponder];
	[keyTextField resignFirstResponder];
	
	if ([[UserDBManager sharedUserDBManager] loginWithName:nameTextField.text andKey:keyTextField.text]) {
		OrderViewController *orderViewController = [[[OrderViewController alloc] initWithNibName:@"OrderView_iPad" bundle:nil] autorelease];
		[self.navigationController pushViewController:orderViewController animated:YES];
	}
}

- (void)viewWillAppear:(BOOL)animated{
	[super viewWillAppear:animated];
	
	[[UserDBManager sharedUserDBManager] signOut];
	keyTextField.text = @"";
	
	[self.navigationController setNavigationBarHidden:YES animated:YES];
    [self.navigationController setToolbarHidden:YES animated:YES];
}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
